
Websites often need work performed outside a visitor’s request: sending queued messages, generating reports, cleaning temporary files, importing data, or running application maintenance. Cron is a Unix scheduling service that starts commands at specified times. Used carefully, it can make these tasks reliable and predictable.
A traditional crontab entry has five time fields followed by a command: minute, hour, day of month, month, and day of week. The command runs when the fields match the current time according to the cron service’s timezone and implementation.
| Expression | Meaning |
|---|---|
*/5 * * * * |
Every five minutes |
0 * * * * |
At the start of every hour |
30 2 * * * |
Daily at 2:30 a.m. |
0 4 * * 0 |
Sunday at 4:00 a.m. |
Day-of-month and day-of-week behavior can vary in ways that surprise administrators, so verify the implementation’s documentation when using both fields. Also confirm the system timezone rather than assuming the schedule follows the website owner’s local time.
Cron runs with a limited environment. A command that works in an interactive shell may fail because PATH, working directory, shell, or environment variables differ. Use absolute paths to executables and scripts, and change to the expected directory explicitly when the application requires it.
cd /home/account/site && /usr/bin/php /home/account/site/scripts/task.php
Confirm the actual PHP binary and script path on the server. Shared hosting environments may provide multiple PHP versions or account-specific binaries.
Uncaptured output can be lost or mailed to an account that nobody checks. Redirect standard output and standard error to a log or configure the hosting panel’s logging features. Logs should be rotated or bounded so a noisy task does not fill the account’s storage.
/usr/bin/php /home/account/site/scripts/task.php >> /home/account/logs/task.log 2>&1
Do not write logs into a publicly accessible web directory unless access is explicitly denied. Logs can contain paths, identifiers, or sensitive application details.
A task that takes longer than its schedule interval can start again before the previous run finishes. Two copies may compete for a lock, duplicate an import, send repeated notifications, or corrupt intermediate files. Use an application-level lock or a tool such as flock where available.
Design tasks to be idempotent when possible: running the same operation twice should not create duplicate or destructive results. For tasks that cannot safely overlap, fail quickly when a prior instance is still active.
Run tasks as frequently as the application needs, not simply as often as the panel allows. Frequent polling can waste resources, while infrequent jobs may delay user-visible work. Consider expected runtime, traffic patterns, rate limits, and provider restrictions.
Some applications trigger scheduled work when a visitor requests a page. That approach can be convenient, but jobs may be delayed on low-traffic sites or triggered unpredictably under load. A real system cron can invoke the application’s scheduler at a fixed interval, if the application supports it.
Follow the application’s own instructions. Do not disable its built-in scheduler or replace it with a custom command unless you understand how its queue and locking behavior work.
Run each task with the least privilege it needs. Avoid placing passwords directly in command lines where they may be exposed in process listings or logs. Protect scripts and configuration files, and ensure a task cannot be invoked by an untrusted user.
Reliable cron jobs depend on correct timing, a predictable execution environment, captured output, and safeguards against duplicate runs. Treat scheduled tasks as production services: monitor them, document their purpose, and test recovery when they fail.